package com.tusuo.stds.ui.pay;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.tencent.mobileqq.openpay.api.IOpenApi;
import com.tencent.mobileqq.openpay.api.IOpenApiListener;
import com.tencent.mobileqq.openpay.api.OpenApiFactory;
import com.tencent.mobileqq.openpay.data.base.BaseResponse;
import com.tencent.mobileqq.openpay.data.pay.PayResponse;
import com.tusuo.stds.Constants;

public class QQPaymentCallBackActivity extends AppCompatActivity implements IOpenApiListener{

    IOpenApi openApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        openApi = OpenApiFactory.getInstance(this, Constants.QQ_APP_ID);
        openApi.handleIntent(getIntent(),this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        openApi.handleIntent(intent, this);
    }

    @Override
    public void onOpenResponse(BaseResponse baseResponse) {

        Intent intent = new Intent("android.intent.action.wxpay");

        if (baseResponse == null) {
            // 不能识别的intent
            intent.putExtra("respCode",-1);
            intent.putExtra("respMsg","空的Response");
            sendBroadcast(intent);
            finish();
        } else {
            if (baseResponse instanceof PayResponse) {
                // 支付回调响应
                PayResponse payResponse = (PayResponse) baseResponse;
                if (payResponse.isSuccess()) {
                    // 支付成功，这个支付结果不能作为发货的依据
                    intent.putExtra("respCode",baseResponse.retCode);
                    sendBroadcast(intent);
                    finish();
                }else if (payResponse.retCode == -1) {
                    intent.putExtra("respCode",-2);
                    sendBroadcast(intent);
                    finish();
                }else {
                    intent.putExtra("respCode",baseResponse.retCode);
                    intent.putExtra("respMsg",payResponse.retMsg);
                    sendBroadcast(intent);
                    finish();
                }
            } else {
                // 不能识别的响应
                intent.putExtra("respCode",baseResponse.retCode);
                intent.putExtra("respMsg","不能识别的Response");
                sendBroadcast(intent);
                finish();
            }
        }
    }
}
